//
//  MenuScene.m
//  Project 5
//
//  Created by Nguyen Tan Loc on 7/6/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "MenuScene.h"
#import "LevelSelectScene.h"

@implementation MenuScene

+(CCScene *) scene
{
	CCScene *scene = [CCScene node];
	MenuScene *layer = [MenuScene node];
	[scene addChild: layer];
	return scene;
}


-(id) init
{
	if(self = [super init])
    {
        [self initMenu];
	}
    
	return self;
}

-(void) initMenu
{
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    
    CCMenu *mMenu = [[CCMenu alloc] init];
    [mMenu setPosition:CGPointZero];
    [self addChild:mMenu];
    
    CCSprite *spriteUpPlay = [CCSprite spriteWithSpriteFrameName:@"buttonRectBig@2x.png"];
    CCSprite *spriteUpFacebook = [CCSprite spriteWithSpriteFrameName:@"coin@2x.png"];
    CCSprite *spriteUpTwitter = [CCSprite spriteWithSpriteFrameName:@"coin@2x.png"];
    
    CCMenuItem *miPlay = [CCMenuItemSprite itemWithNormalSprite:spriteUpPlay selectedSprite:nil target:self selector:@selector(changeLevelSelectScene)];
    [miPlay setPosition:ccp(winSize.width / 2, winSize.height / 2)];
    [mMenu addChild:miPlay];
    
    CCMenuItem *miFacebook = [CCMenuItemSprite itemWithNormalSprite:spriteUpFacebook selectedSprite:nil target:self selector:@selector(openFacebookFanpage)];
    [miFacebook setPosition:ccp(winSize.width - 100, 50)];
    [mMenu addChild:miFacebook];
    
    CCMenuItem *miTwitter = [CCMenuItemSprite itemWithNormalSprite:spriteUpTwitter selectedSprite:nil target:self selector:@selector(openTwitterFanpage)];
    [miTwitter setPosition:ccp(winSize.width - 40, 50)];
    [mMenu addChild:miTwitter];
}

-(void) changeLevelSelectScene
{
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[LevelSelectScene scene]]];
}

-(void) openFacebookFanpage
{
    NSURL *urlFanpage = [NSURL URLWithString:@"fb://profile/221824281219838"];
    if (![[UIApplication sharedApplication] openURL: urlFanpage])
    {
        NSURL *webURL = [NSURL URLWithString:@"http://www.facebook.com/pages/MindBoxz/221824281219838"];
        [[UIApplication sharedApplication] openURL: webURL];
    }
}

-(void) openTwitterFanpage
{
    NSURL *urlFanpage = [NSURL URLWithString:@"twitter://user?screen_name=9to5mac"];
    if (![[UIApplication sharedApplication] openURL: urlFanpage])
    {
        NSURL *webURL = [NSURL URLWithString:@"https://twitter.com/9to5mac"];
        [[UIApplication sharedApplication] openURL: webURL];
    }
    
}

@end
